/*
 * Created by JFormDesigner on Fri Jul 04 20:07:57 CST 2014
 */

package com.adcc.swingui.demo.set;

import java.awt.*;
import java.util.HashSet;

import javax.swing.*;
import javax.swing.border.*;
import com.adcc.swingui.base.*;
import com.adcc.swingui.demo.set.model.SetUplinkModel;
import com.adcc.swingui.serialize.ISerializeObjectSavedListener;
import com.adcc.swingui.serialize.SerializeObjectFactory;

/**
 * @author charlie
 */
public class SetUplinkPanel extends DefaultOptionsPanel {

	private static final long serialVersionUID = 1L;

	private SetUplinkModel model = null;

	public SetUplinkPanel(String name, Container container) {
		super(name, container);
		initComponents();
		labTitle.setText(name);
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY
		// //GEN-BEGIN:initComponents
		labTitle = new JLabel();
		panel1 = new JPanel();
		panel2 = new JPanel();
		label2 = new JLabel();
		timeOut = new JTextField();
		label3 = new JLabel();

		//======== this ========
		setLayout(new BorderLayout());

		//---- labTitle ----
		labTitle.setText("\u4e0a\u884c\u62a5\u6587");
		add(labTitle, BorderLayout.NORTH);

		//======== panel1 ========
		{
			panel1.setLayout(new BorderLayout());

			//======== panel2 ========
			{
				panel2.setBorder(new TitledBorder("\u8d85\u65f6\u8bbe\u7f6e"));
				panel2.setLayout(new FlowLayout(FlowLayout.LEFT));

				//---- label2 ----
				label2.setText("\u8d85\u65f6\u65f6\u95f4");
				panel2.add(label2);

				//---- timeOut ----
				timeOut.setPreferredSize(new Dimension(30, 21));
				panel2.add(timeOut);

				//---- label3 ----
				label3.setText("\u5206\u949f ");
				panel2.add(label3);
			}
			panel1.add(panel2, BorderLayout.NORTH);
		}
		add(panel1, BorderLayout.CENTER);
		// //GEN-END:initComponents
	}

	@Override
	public void lazyInitialize() {
		// 注册model
		registerModel();
		// 加载model中数据
		restoreModel();
		// 设置加载完毕状态
		inited = true;
	}

	@Override
	public boolean apply() {
		boolean flag = true;
		String time = timeOut.getText();
		if (!time.equals(model.getUplinkTimeOut())) {
			model.setUplinkTimeOut(time);
			model.setPropertyChange(true);
		}
		if (model.isPropertyChange()) {
			// 保存应用
			flag = model.save("admin");
		}
		model.setPropertyChange(false);
		return flag;
	}

	@Override
	public void restoreDefault() {
		model.restoreDefault();
		restoreModel();
	}

	@Override
	public void restoreModel() {
		String uplinkTimeOut = model.getUplinkTimeOut();
		timeOut.setText(uplinkTimeOut);
	}

	@Override
	public void i18n() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void registerModel() {
		model = (SetUplinkModel) SerializeObjectFactory.getSerializeObject(
				SetUplinkModel.class.getName(), "admin");
		// 注册唯一事件
		model.addOneSavedListener(new ISerializeObjectSavedListener() {
			public void onPropertySaved(HashSet<String> arg0) {
				// 如果是比较慢的操作，建议放入线程
			}
		});
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY
	// //GEN-BEGIN:variables
	private JLabel labTitle;
	private JPanel panel1;
	private JPanel panel2;
	private JLabel label2;
	private JTextField timeOut;
	private JLabel label3;
	// JFormDesigner - End of variables declaration //GEN-END:variables
}
